<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of seriesHistory
 *
 * @author Alex
 */
class seriesHistory {

    private $seriesData = Array(), $winnerData = Array();
    private $headerObject, $mode, $id, $divcount = 0;

    function seriesHistory($mode, $data)
    {
        $this->mode = $mode;
        
        include_once('includes/page_header.php');
        
        global $db, $lang;
   
        $this->id = $data['c_series'];

        //get series info
        $query = "SELECT s_id, s_title, s_about, s_logo, s_logoThmb, c_title, c_id
                  FROM " . SERIES_TABLE . ", " . CHAMPIONSHIPS_TABLE . "
                  WHERE s_id = c_series and s_id = {$this->id}
                  ORDER BY c_lastRace desc
                  LIMIT 0, 1";

        $res = $db->sql_query($query);
        if ($row = $db->sql_fetchrow($res))
            $this->seriesData = $row;
        else
            throw new Exception("Unable to get Series Data");

        $headerSubTitle = $this->getModeTitle() . '<br/><br/>' . $this->seriesData['s_about'] . '<br/><br/>' .
                sprintf($lang['SeriesLatestChampionship'],"<a href=\"championships.php?id={$this->seriesData['c_id']}\">{$this->seriesData['c_title']}</a>");

        $this->headerObject = new page_header($this->seriesData, 's_title', '', 's_logo', '', $headerSubTitle);


        //get winner info
        $query = $this->getModeDataQuery();
        
        $res = $db->sql_query($query);
        while ($row = $db->sql_fetchrow($res))
            $this->addWinnerInfo($row);

    }

    function getModeTitle()
    {
        global $lang;

        if($this->mode=='teams')
        {
            return sprintf($lang['ItemHistory'],$lang['Teams']);
        }
        else if($this->mode=='manufacturers')
        {
            return sprintf($lang['ItemHistory'],$lang['Manufacturers']);
        }
        else
        {
            return sprintf($lang['ItemHistory'],$lang['Drivers']);
        }
    }

    function getModeDataQuery()
    {
        if($this->mode=='teams')
        {
            return "SELECT c_id, c_title, c_smallTitle, c_logoThmbURL, d_title, d_order, d_logoThmb, hc_title, he_rank, t_id, t_name, t_logo, t_logoThmb,
                         (SELECT group_concat(
                                        CONCAT('<a href=&quot;drivers.php?id=', user_id, '&quot;>', pf_drivername, '</a>') SEPARATOR ', ')
                                 FROM " . DRIVERNAME_TABLE . ", " . CHAMPSIGNUPS_TABLE . "
                                 WHERE cs_driver = user_id AND cs_team = t_id AND cs_div = hc_division) AS teammembers,
                         (SELECT group_concat(DISTINCT
                                        CONCAT('<a href=&quot;cars.php?id=', c_id, '&quot;>', c_manufacturer, ' ', c_model, '</a>') SEPARATOR ', ')
                                 FROM " . CARS_TABLE . ", " . CHAMPSIGNUPS_TABLE . "
                                 WHERE cs_team = t_id  AND c_id = cs_car AND cs_div = hc_division GROUP BY c_id) AS cardetails
                     FROM ". SERIES_TABLE .", " . CHAMPIONSHIPS_TABLE . ", " . DIVISIONS_TABLE . ", " .
                             HONOURENTRIES_TABLE . ", " . HONOURCATS_TABLE . ", " . TEAMS_TABLE . "
                     WHERE s_id = {$this->id} AND s_id = c_series AND d_champid = c_id AND d_id = hc_division AND hc_cat = 1 AND
                           he_cat = hc_id AND he_item = t_id
                     ORDER BY c_lastRace DESC, d_order, he_rank";
        }
        else if($this->mode=='manufacturers')
        {
            return
                "SELECT c.c_id, c.c_title, c.c_smallTitle, c.c_logoThmbURL, d_title, d_order, d_logoThmb,  hc_title, he_rank,
                        car.c_id as car_id, car.c_model, car.c_manufacturer, car.c_logo as car_logo, car.c_logoThmb as car_logoThmb
                 FROM ". SERIES_TABLE . ", " . CHAMPIONSHIPS_TABLE . " c, " . DIVISIONS_TABLE . ", " . HONOURCATS_TABLE . ", " .
                         HONOURENTRIES_TABLE . ", " . CARS_TABLE . " car
                 WHERE s_id = {$this->id} AND s_id = c.c_series AND d_champid = c.c_id AND d_id = hc_division AND hc_cat = 2 AND
                       he_cat = hc_id AND he_item = car.c_id
                 ORDER BY c.c_lastRace DESC, d_order, he_rank";
        }
        else
        {
            return
                "SELECT c.c_id, c.c_title, c.c_smallTitle, c.c_logoThmbURL, d_title, d_order, d_logoThmb,  hc_title, he_rank, pf_drivername,
                        user_id, cs_imageThmb, cs_image, t_name, t_id, t_logo, t_logoThmb, car.c_id as car_id, car.c_model,
                        car.c_manufacturer, car.c_logo as car_logo, car.c_logoThmb as car_logoThmb
                 FROM ". SERIES_TABLE . ", " . CHAMPIONSHIPS_TABLE . " c, " . DIVISIONS_TABLE . ", " . HONOURCATS_TABLE . ", " .
                         HONOURENTRIES_TABLE . ", " . DRIVERNAME_TABLE . ", " . CARS_TABLE . " car, " . CHAMPSIGNUPS_TABLE . "
                 LEFT JOIN " . TEAMS_TABLE . " ON cs_team = t_id
                 WHERE s_id = {$this->id} AND s_id = c.c_series AND d_champid = c.c_id AND d_id = hc_division AND hc_cat = 0 AND
                       he_cat = hc_id AND he_item = user_id AND cs_driver = user_id AND cs_champ = c.c_id AND cs_car = car.c_id
                 ORDER BY c.c_lastRace DESC, d_order, he_rank";
        }
    }


    function addWinnerInfo($row)
    {
        global $lang;

        //how many divisions?
        if($row['d_order'] > $this->divcount)
            $this->divcount = $row['d_order'];

        $this->winnerData[$row['c_id']]['c_title'] = $row['c_title'];
        $this->winnerData[$row['c_id']]['c_smallTitle'] = $row['c_smallTitle'];
        $this->winnerData[$row['c_id']]['c_logoThmbURL'] = $row['c_logoThmbURL'];
        $this->winnerData[$row['c_id']]['division' . $row['d_order']]['d_title'] = $row['d_title'];
        $this->winnerData[$row['c_id']]['division' . $row['d_order']]['d_order'] = $row['d_order'];
        $this->winnerData[$row['c_id']]['division' . $row['d_order']]['d_logoThmb'] = $row['d_logoThmb'];
        $this->winnerData[$row['c_id']]['division' . $row['d_order']]['hc_title'] = $row['hc_title'];

        if($this->mode=='teams')
        {
            $this->winnerData[$row['c_id']]['division' . $row['d_order']]['honour' . $row['he_rank']]['t_name'] = $row['t_name'];
            $this->winnerData[$row['c_id']]['division' . $row['d_order']]['honour' . $row['he_rank']]['t_id'] = $row['t_id'];
            $this->winnerData[$row['c_id']]['division' . $row['d_order']]['honour' . $row['he_rank']]['t_logo'] = $row['t_logo'];
            $this->winnerData[$row['c_id']]['division' . $row['d_order']]['honour' . $row['he_rank']]['t_logoThmb'] = $row['t_logoThmb'];
            $this->winnerData[$row['c_id']]['division' . $row['d_order']]['honour' . $row['he_rank']]['teammembers'] = $row['teammembers'];
            $this->winnerData[$row['c_id']]['division' . $row['d_order']]['honour' . $row['he_rank']]['cardetails'] = $row['cardetails'];
        }
        else if($this->mode=='manufacturers')
        {
            $this->winnerData[$row['c_id']]['division' . $row['d_order']]['honour' . $row['he_rank']]['car_id'] = $row['car_id'];
            $this->winnerData[$row['c_id']]['division' . $row['d_order']]['honour' . $row['he_rank']]['car_model'] = $row['c_model'];
            $this->winnerData[$row['c_id']]['division' . $row['d_order']]['honour' . $row['he_rank']]['car_manufacturer'] = $row['c_manufacturer'];
            $this->winnerData[$row['c_id']]['division' . $row['d_order']]['honour' . $row['he_rank']]['car_logo'] = $row['car_logo'];
            $this->winnerData[$row['c_id']]['division' . $row['d_order']]['honour' . $row['he_rank']]['car_logoThmb'] = $row['car_logoThmb'];
        }
        else
        {
            $this->winnerData[$row['c_id']]['division' . $row['d_order']]['honour' . $row['he_rank']]['u_id'] = $row['user_id'];
            $this->winnerData[$row['c_id']]['division' . $row['d_order']]['honour' . $row['he_rank']]['u_drivername'] = $row['pf_drivername'];
            $this->winnerData[$row['c_id']]['division' . $row['d_order']]['honour' . $row['he_rank']]['u_image'] = $row['cs_image'];
            $this->winnerData[$row['c_id']]['division' . $row['d_order']]['honour' . $row['he_rank']]['u_imageThmb'] = $row['cs_imageThmb'];
            $this->winnerData[$row['c_id']]['division' . $row['d_order']]['honour' . $row['he_rank']]['t_id'] = $row['t_id'];
            $this->winnerData[$row['c_id']]['division' . $row['d_order']]['honour' . $row['he_rank']]['t_logo'] = $row['t_logo'];
            $this->winnerData[$row['c_id']]['division' . $row['d_order']]['honour' . $row['he_rank']]['t_logoThmb'] = $row['t_logoThmb'];
            if(is_numeric($row['t_id']) && $row['t_id'] > 0)
                $this->winnerData[$row['c_id']]['division' . $row['d_order']]['honour' . $row['he_rank']]['t_name'] = $row['t_name'];
            else
                $this->winnerData[$row['c_id']]['division' . $row['d_order']]['honour' . $row['he_rank']]['t_name'] = $lang['Independent'];
            $this->winnerData[$row['c_id']]['division' . $row['d_order']]['honour' . $row['he_rank']]['car_id'] = $row['car_id'];
            $this->winnerData[$row['c_id']]['division' . $row['d_order']]['honour' . $row['he_rank']]['car_model'] = $row['c_model'];
            $this->winnerData[$row['c_id']]['division' . $row['d_order']]['honour' . $row['he_rank']]['car_manufacturer'] = $row['c_manufacturer'];
            $this->winnerData[$row['c_id']]['division' . $row['d_order']]['honour' . $row['he_rank']]['car_logo'] = $row['car_logo'];
            $this->winnerData[$row['c_id']]['division' . $row['d_order']]['honour' . $row['he_rank']]['car_logoThmb'] = $row['car_logoThmb'];
        }
    }

    function renderHonourEntrant($data, $rank)
    {
        if(isset($data))
        {
            global $image_dir;

            $return = '';
            if($this->mode=='teams')
            {
                if($data['t_logo'] != '')
                    $return = '<p align=&quot;center&quot;><img src=&quot;' . $image_dir . $data['t_logo'] . '&quot;/><br>';
                else
                    $return = '<p align=&quot;center&quot;>';

                $return .=  "{$data['t_name']}<br/>{$data['teammembers']}<br/>{$data['cardetails']}<br/></p>";

                $return = "<a href=\"teams.php?id={$data['t_id']}\" onmouseover=\"Tip('{$return}',
                    BGCOLOR, '#FFFFFF', BORDERCOLOR, '#000000', STICKY, true, FADEIN, 500, FADEOUT, 500)\" onmouseout=\"UnTip()\">
                                            {$rank}. {$data['t_name']}</a>";
            }
            else if($this->mode=='manufacturers')
            {
                if($data['car_logo'] != '')
                    $return = '<p align=&quot;center&quot;><img src=&quot;' . $image_dir . $data['car_logo'] . '&quot;/><br>';
                else
                    $return = '<p align=&quot;center&quot;>';

                $return .=  "{$data['car_manufacturer']} {$data['car_model']}<br/></p>";

                $return = "<a href=\"cars.php?id={$data['car_id']}\" onmouseover=\"Tip('{$return}',
                    BGCOLOR, '#FFFFFF', BORDERCOLOR, '#000000', STICKY, true, FADEIN, 500, FADEOUT, 500)\" onmouseout=\"UnTip()\">
                                            {$rank}. {$data['car_manufacturer']} {$data['car_model']}</a>";
            }
            else
            {
                if($data['u_imageThmb'] != '')
                    $return = '<p align=&quot;center&quot;><img src=&quot;' . $image_dir . $data['u_imageThmb'] . '&quot;/><br>';
                else if($data['t_logo'] != '')
                    $return = '<p align=&quot;center&quot;><img src=&quot;' . $image_dir . $data['t_logo'] . '&quot;/><br>';
                else if($data['car_logo'] != '')
                    $return = '<p align=&quot;center&quot;><img src=&quot;' . $image_dir . $data['car_logo'] . '&quot;/><br>';
                else
                    $return = '<p align=&quot;center&quot;>';

                $return .=  "{$data['u_drivername']}<br/>{$data['t_name']}<br/>{$data['car_manufacturer']} {$data['car_model']}<br/></p>";

                $return = "<a href=\"drivers.php?id={$data['u_id']}\" onmouseover=\"Tip('{$return}',
                    BGCOLOR, '#FFFFFF', BORDERCOLOR, '#000000', STICKY, true, FADEIN, 500, FADEOUT, 500)\" onmouseout=\"UnTip()\">
                                            {$rank}. {$data['u_drivername']}</a>";
            }
            return $return;
        }
        else
            return "";
    }


    function render()
    {
        $this->headerObject->render();

        global $skin_dir, $lang;

        include($skin_dir . 'tpl/tableStart.tpl');
        
        if(count($this->winnerData) == 0)
        {
            include($skin_dir . 'tpl/rowStart.tpl');
            $width='';
            $class='tableInfo';
            $value=sprintf($lang['SeriesNoChampions'],$this->seriesData['s_title']);
            include($skin_dir . 'tpl/tableCell.tpl');
            include($skin_dir . 'tpl/rowEnd.tpl');
        }
        else
        {
            foreach($this->winnerData as $champid => $champ)
            {
                include($skin_dir . 'tpl/rowStart.tpl');

                $width='width="20%"';
                $class='tableHeading';
                $value=$champ['c_logoThmbURL']!=""?$champ['c_smallTitle']:"";
                include($skin_dir . 'tpl/tableCell.tpl');

                $otherwidth = 80 / $this->divcount;

                for($x = 1; $x <= $this->divcount; $x++)
                {
                    $width="width=\"{$otherwidth}%\"";
                    $class='tableHeadingLeft';
                    $value='&nbsp;&nbsp;'.$champ['division' . $x]['d_title'];
                    include($skin_dir . 'tpl/tableCell.tpl');
                }


                include($skin_dir . 'tpl/rowStart.tpl');
                include($skin_dir . 'tpl/rowEnd.tpl');

                //champ logo
                $width="align=\"center\" title=\"{$champ['c_title']}\" ";
                $class='tableImageFree';
                $value=$champ['c_logoThmbURL']!="" ?
                        "<a href=\"championships.php?id={$champid}\"><img src=\"{$champ['c_logoThmbURL']}\"></a>" :
                        "<a href=\"championships.php?id={$champid}\">{$champ['c_smallTitle']}></a>" ;
                include($skin_dir . 'tpl/tableCell.tpl');


                for($x = 1; $x <= $this->divcount; $x++)
                {
                    $width='';
                    $class='tableInfo';
                    $value='';
                    if(isset($champ['division'.$x]['honour1']))
                        $value .= $this->renderHonourEntrant($champ['division'.$x]['honour1'], 1) . '<br/>';

                    if(isset($champ['division'.$x]['honour2']))
                        $value .= $this->renderHonourEntrant($champ['division'.$x]['honour2'], 2) . '<br/>';

                    if(isset($champ['division'.$x]['honour3']))
                        $value .= $this->renderHonourEntrant($champ['division'.$x]['honour3'], 3);

                    include($skin_dir . 'tpl/tableCell.tpl');
                }

                include($skin_dir . 'tpl/rowEnd.tpl');
            }

        }

        include($skin_dir . 'tpl/tableEnd.tpl');
    }
}
?>
